
/**   
*    
* 项目名称：a_yongan   
* 类名称：OutsourcingInServiceImpl   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年6月30日 上午10:59:50    
* 修改时间：2018年6月30日 上午10:59:50   
* 修改备注：   
* @version    
*    
*/
package com.yongan.warehouse.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import com.yongan.user.enums.UserStatEnum;
import com.yongan.user.exception.UserException;
import com.yongan.util.Execution;
import com.yongan.warehouse.dao.GoodsinformationDao;
import com.yongan.warehouse.dao.OtherGoodOutDao;
import com.yongan.warehouse.dao.OutGoodDetailsDao;
import com.yongan.warehouse.entity.OtherGoodOut;
import com.yongan.warehouse.entity.OutGoodDetails;
import com.yongan.warehouse.service.OtherGoodOutService;

/**
 * @author liubo
 *
 */
@Service
public class OtherGoodOutServiceImpl implements OtherGoodOutService{

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private OtherGoodOutDao otherGoodOutDao;
	@Autowired
	private OutGoodDetailsDao outGoodDetailsDao;
	@Autowired
	private GoodsinformationDao goodsinformationDao;
	private final String slat = "dsjdajsdkHJJHAJAKdsd921e0p12*DAe#@$@!qew";
	@Override
	public List<OtherGoodOut> getOtherGoodOutList(int offset, int limit) {
		return otherGoodOutDao.queryAll(offset, limit);
	}

	
	@Override
	public OtherGoodOut getOtherGoodOutById(String otherGoodOutId) {
		// TODO 自动生成的方法存根
		OtherGoodOut otherGoodOut = otherGoodOutDao.queryById(otherGoodOutId);
		otherGoodOut.setOutGoodDetailsList(outGoodDetailsDao.queryAllByTopId(otherGoodOutId));
		return otherGoodOut;
	}

	

	@Override
	@Transactional
	public Execution executeOtherGoodOut(OtherGoodOut otherGoodOut,
			String otherGoodOutId, String md5) throws UserException {
		if(md5 == null || !md5.equals(getMD5(otherGoodOutId))){
			//throw new UserException("user data rewrite");
		}
		//System.out.println("otherGoodOutId:"+otherGoodOutId);
		//System.out.println(otherGoodOut.toString());
		List<OutGoodDetails> outGoodDetails =null;
		try{
			if(otherGoodOut == null){
				OtherGoodOut readDell = otherGoodOutDao.queryById(otherGoodOutId);
				outGoodDetails = readDell.getOutGoodDetailsList();
				if(!(outGoodDetails==null)){
					for(OutGoodDetails goodDetails:outGoodDetails){
						reduceNumber(goodDetails, "add");
					}
				}
				int deleteCount = otherGoodOutDao.deleteOtherGoodOut(otherGoodOutId);
				int deleteInGoodDetailsCount = outGoodDetailsDao.deleteAllOutGoodDetails(otherGoodOutId);
				if(deleteCount<=0 & deleteInGoodDetailsCount<=0){
					throw new UserException("delete failed");
				}else{
					return new Execution(otherGoodOutId, UserStatEnum.SUCCESS);
				}
			}else if((otherGoodOutId.equals(""))){
				Integer hjrksl= 0 ;
				Integer hjsl= 0 ;
				Integer zje= 0 ;
				
				outGoodDetails = otherGoodOut.getOutGoodDetailsList();
				int goodDetailsaddcount = 1;
				if(!(outGoodDetails==null)){
					for(OutGoodDetails goodDetails:outGoodDetails){
						hjrksl = hjrksl + Integer.parseInt(goodDetails.getOutgooddetailsCksl());
						hjsl = hjsl + Integer.parseInt(goodDetails.getOutgooddetailsSl());
						zje = zje + Integer.parseInt(goodDetails.getOutgooddetailsZje());
						goodDetails.setOutgooddetailsId(UUID.randomUUID().toString());
						goodDetails.setTopId(otherGoodOut.getOthergoodoutId());
						goodDetails.setOutgooddetailsType("qtckd");
						int addInGoodDetailsCount = outGoodDetailsDao.insertOutGoodDetails(goodDetails);
						if(addInGoodDetailsCount<=0){
							goodDetailsaddcount = addInGoodDetailsCount;
						}
						reduceNumber(goodDetails, "reduce");
					}
				}
				otherGoodOut.setOthergoodoutHjrksl(String.valueOf(hjrksl));
				otherGoodOut.setOthergoodoutHjsl(String.valueOf(hjsl));
				otherGoodOut.setOthergoodoutHjzje(String.valueOf(zje));
				int addCount = otherGoodOutDao.insertOtherGoodOut(otherGoodOut);
				if(addCount<=0 & goodDetailsaddcount<=0){
					throw new UserException("add failed");
				}else{
					return new Execution(otherGoodOut.getOthergoodoutId(), UserStatEnum.SUCCESS);
				}
			}else if(!(otherGoodOutId.equals(""))){
				List<OutGoodDetails> oldList = outGoodDetailsDao.queryAllByTopId(otherGoodOutId);
				List<String> newList = new ArrayList<String>();
				//String[] newList;
				outGoodDetails = otherGoodOut.getOutGoodDetailsList();
				int goodDetailscount = 1;
				Integer hjrksl= 0 ;
				Integer hjsl= 0 ;
				Integer zje= 0 ;
				if(!(outGoodDetails==null)){
					for(OutGoodDetails goodDetails:outGoodDetails){
						//减去新保存的列表
						reduceNumber(goodDetails, "reduce");
						hjrksl = hjrksl + Integer.parseInt(goodDetails.getOutgooddetailsCksl());
						hjsl = hjsl + Integer.parseInt(goodDetails.getOutgooddetailsSl());
						zje = zje + Integer.parseInt(goodDetails.getOutgooddetailsZje());
						//如果更新的总单据下面的子单据ID为空，则为新增，否则是修改
						if(goodDetails.getOutgooddetailsId().equals("")){
							goodDetails.setOutgooddetailsId(UUID.randomUUID().toString());
							goodDetails.setTopId(otherGoodOut.getOthergoodoutId());
							goodDetails.setOutgooddetailsType("qtckd");
							int ua = outGoodDetailsDao.insertOutGoodDetails(goodDetails);
							if(ua<=0){
								goodDetailscount = ua;
							}
						}else{
							//reduceNumber(goodDetails, "reduce");
							int uu = outGoodDetailsDao.updateOutGoodDetails(goodDetails);
							if(uu<=0){
								goodDetailscount = uu;
							}else{
								newList.add(goodDetails.getOutgooddetailsId());
							}
						}
					}
				}
				if (oldList != null) {
					for (OutGoodDetails old : oldList) {
						// 减去旧列表在库存中的数量
						reduceNumber(old, "add");
						Boolean sf = false;
						if (newList != null) {
							for (String newl : newList) {
								if (old.getOutgooddetailsId().equals(newl)) {
									sf = true;
								}
							}
							if (!sf) {
								outGoodDetailsDao.deleteOutGoodDetails(old.getOutgooddetailsId());
							}
						}
					}
				}

				otherGoodOut.setOthergoodoutHjrksl(String.valueOf(hjrksl));
				otherGoodOut.setOthergoodoutHjsl(String.valueOf(hjsl));
				otherGoodOut.setOthergoodoutHjzje(String.valueOf(zje));
				int updateCount = otherGoodOutDao.updateOtherGoodOut(otherGoodOut);
				if(updateCount<=0 & goodDetailscount<=0){
					throw new UserException("update failed");
				}else{
					return new Execution(otherGoodOutId, UserStatEnum.SUCCESS);
				}
			}else{
				throw new UserException("parameter error");
			}
			
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			throw new UserException("inner error:"+e.getMessage());
		}
	}

	
	@Override
	public int getCount() {
		// TODO 自动生成的方法存根
		return otherGoodOutDao.queryCount();
	}
	
	private String getMD5(String Id){
		String base = Id + "/" + slat;
		String md5 = DigestUtils.md5DigestAsHex(base.getBytes());
		return md5;
	}
	
	public OtherGoodOutServiceImpl() {
		super();
		// TODO 自动生成的构造函数存根
	}


	public int reduceNumber(OutGoodDetails igd,  String type){
		return goodsinformationDao.reduceNumber(igd.getOutgooddetailsNumber()
				, type, Integer.parseInt(igd.getOutgooddetailsSl())
				, Integer.parseInt(igd.getOutgooddetailsCksl()));
	}
	
}
